<?php
namespace app\appclass\controller;
use app\AdminRest;

use app\ApiRest;
use app\appclass\model\CardList;
use app\appclass\model\CardUser;
use app\appclass\model\CardWater;
use app\appclass\model\ClassCard;
use app\appclass\model\ClassCate;
use app\appclass\model\ClassCoach;
use app\appclass\model\ClassDate;
use app\appclass\model\ClassLable;
use app\appclass\model\ClassList;
use app\appclass\model\ClassRecord;
use app\appclass\model\ClassRoom;
use app\appclass\model\Lable;
use app\appclass\model\StoreInfo;
use app\card\model\Company;
use app\shop\model\IndexUser;
use app\shop\model\IndexUserInfo;
use think\App;
use think\facade\Db;


class IndexStoreClass extends ApiRest
{

    protected $model;

    protected $record_model;

    protected $lable_model;

    protected $card_user_model;

    protected $date_model;

    protected $store_id;

    protected $card_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model       = new ClassList();

        $this->record_model= new ClassRecord();

        $this->lable_model = new ClassLable();

        $this->card_model = new CardList();

        $this->card_user_model  = new CardUser();

        $this->date_model  = new ClassDate();

        $this->store_id = $this->model->getStoreId($this->getUserId());

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 17:50
     * @功能说明:课程列表
     */

    public function classList(){

        $input = $this->_param;

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $dis['store_id'] = $this->store_id;

        if(!empty($input['cate_id'])){

            $dis['cate_id'] = $input['cate_id'];
        }

        $data = $this->model->dataList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 10:45
     * @功能说明:添加课程
     */
    public function classAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['store_id'] = $this->store_id;

        $res = $this->model->adminDataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 10:48
     * @功能说明:编辑课程
     */
    public function classUpdate(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->model->adminDataUpdate($dis,$input);

        return $this->success($res);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-13 14:35
     * @功能说明:课程上下架
     */
    public function classStatusUpdate(){

        $input = $this->_input;


        $dis = [

            'id' => $input['id']
        ];

        $res = $this->model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 10:52
     * @功能说明:课程详情
     */
    public function classInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->model->dataInfo($dis);

        $class_lable_model = new ClassLable();

        $cate_model = new ClassCate();

        $res['lable'] = $class_lable_model->classLableList($res['id']);

        $res['cate_type'] = $cate_model->where(['id'=>$res['cate_id']])->value('type');

        if(!empty($res['class_card'])){

            $card_model = new CardList();

            foreach ($res['class_card'] as &$v){

                $v['card_name'] = $card_model->where(['id'=>$v['card_id']])->value('title');

                $v['card_type'] = $card_model->where(['id'=>$v['card_id']])->value('type');

            }
        }

        if(!empty($res['coach'])){

            foreach ($res['coach'] as &$v){

                $v['avatar'] = lbUserAvatar($v['coach_id']);

            }

        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 10:22
     * @功能说明:教练列表
     */
    public function coachSelect(){

        $input = $this->_param;
        //1就是查所有 0就是查正在上班的 默认0
        $is_all = !empty($input['is_all'])?$input['is_all']:0;

        $card_model = new CardList();

        $dis = [];

        if(!empty($input['name'])){

            $dis[] = ['name','like','%'.$input['name'].'%'];

            $dis[] = ['phone','like','%'.$input['name'].'%'];
        }

        $data = $card_model->coach($this->store_id,$dis,$is_all);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 10:24
     * @功能说明:教室列表
     */
    public function classroomSelect(){

        $input = $this->_param;

        $room_model = new ClassRoom();

        $dis = [

            'uniacid' => $this->_uniacid,

            'store_id'=> $this->store_id,

            'status'  => 1
        ];

        $data = $room_model->where($dis)->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 10:36
     * @功能说明:标签列表
     */
    public function lableSelect(){

        $input = $this->_param;

        $lable_model = new Lable();

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1,

            'pid'     => 0
        ];

        $data = $lable_model->where($dis)->group('id desc')->select()->toArray();

        if(!empty($data)){

            foreach ($data as &$v){

                $v['list'] = $lable_model->where(['pid'=>$v['id']])->where('status','=',1)->order('id desc')->select()->toArray();

            }

        }

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 10:40
     * @功能说明:类型下拉框
     */
    public function cateSelect(){

        $input = $this->_param;

        $cate_model = new ClassCate();

        $dis = [

            'uniacid' => $this->_uniacid,

            'store_id'=> $this->store_id,

            'status'  => 1
        ];

        $data = $cate_model->where($dis)->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 17:48
     * @功能说明:会员卡下拉框
     */
    public function cardSelect(){

        $input = $this->_param;

        $dis = [

            'store_id' => $this->store_id,

            'status'   => 1,

            'uniacid'  => $this->_uniacid
        ];

        $type = [0,1,2];

        foreach ($type as $k=> $value){

            $dis['type'] = $value;

            $data[$k]['children'] = $this->card_model->where($dis)->order('top desc,id desc')->select()->toArray();

            $data[$k]['id'] = $value;

            if($value==0){

                $data[$k]['title'] = '期限卡';

            }elseif ($value==1){

                $data[$k]['title'] = '次数卡';

            }elseif ($value==2){

                $data[$k]['title'] = '储值卡';

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-04 16:41
     * @功能说明:课程排课列表
     */
    public function classDateList(){

        $this->date_model->cancelfailClass($this->_uniacid);

        $input = $this->_param;

        $dis = [

            'store_id' => $this->store_id
        ];

        $cate_model = new ClassCate();

        $list = $this->date_model->where($dis)->where('status','>',-1)->whereDay('start_time',$input['day'])->group('start_time')->field('start_time')->select()->toArray();

        if(!empty($list)){

            foreach ($list as &$value){

                $dis['start_time'] = $value['start_time'];

                $class = $this->date_model->where($dis)->where('status','>',-1)->select()->toArray();

                foreach ($class as &$v){

                    $v['cate_name'] = $cate_model->where(['id'=>$v['cate_id']])->value('title');

                    $v['time_text'] = date('H:i',$v['start_time']).'-'.date('H:i',$v['end_time']);

                }

                $value['start_time'] = date('H:i',$value['start_time']);

                $value['list'] = $class;

            }

        }

        return $this->success($list);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-01 14:17
     * @功能说明:私有课教师上课明细
     */
    public function privateCoachClassInfo(){

        $input = $this->_param;

        $dis[] = ['coach_id','=',$input['coach_id']];

        if(!empty($input['day'])){

            $start_time = strtotime($input['day']);

            $end_time   = $start_time+86400-1;

            $dis[] = ['start_time','between',"$start_time,$end_time"];
        }

        $dis[] = ['class_type','=',1];

        $data = $this->record_model->staffIndexList($dis);

        $class['coach_name'] = lbUserName($input['coach_id']);

        $class['day'] = $input['day'];

        $list['user_list']  = $data;

        $list['class'] = $class;

        return $this->success($list);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-13 11:47
     * @功能说明:添加课程排课
     */
    public function classDateAdd(){

        $input = $this->_input;

        $input['uniacid']  = $this->_uniacid;

        $input['store_id'] = $this->store_id;

        $res = $this->date_model->classDataAdd($input);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 11:59
     * @功能说明:编辑排课
     */

    public function classDateUpdate(){

        $input = $this->_input;

        $res = $this->date_model->classDataSave($input,$input['start_time'],$input['end_time']);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 13:39
     * @功能说明:排课详情
     */
    public function classDateInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->date_model->dataInfo($dis);

        $room_model = new ClassRoom();

        $data['room_name'] = $room_model->where(['id'=>$data['room_id']])->value('title');

        return $this->success($data);


    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-13 17:03
     * @功能说明:课程列表
     */
    public function classSelect(){

        $input = $this->_param;

        $cate_model = new ClassCate();

        $dis = [

            'uniacid' => $this->_uniacid,

            'store_id'=> $this->store_id,

            'status'  =>1
        ];

        if(isset($input['type'])){

            $dis['type'] = $input['type'];
        }

        $list = $cate_model->where($dis)->order('id desc')->select()->toArray();

        if(!empty($list)){

            foreach ($list as $k=> $value){

                $dis = [

                    'status' => 1,

                    'uniacid'=> $this->_uniacid,

                    'cate_id'=> $value['id']
                ];

                $class = $this->model->where($dis)->order('top desc,id desc')->select()->toArray();

                if(!empty($class)){

                    $data[$k]['title'] = $value['title'];

                    $data[$k]['id']    = $value['id'];

                    $data[$k]['children'] = $class;
                }

            }

        }

        $data = !empty($data)?array_values($data):[];

        return $this->success($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-13 10:22
     * @功能说明:教练列表
     */
    public function coachList(){

        $input = $this->_param;

        $card_model = new CardList();

        $dis[] = ['class_status','=',1];

        if(!empty($name)){

            $dis[] = ['name','like','%'.$name.'%'];
        }
        //只查询含有私教课的教练
//        if($private==1){
//
//            $coach_id = $this->privateClassCoach($uniacid);
//
//            $dis[] = ['fans_id','in',$coach_id];
//
//        }

        $data = $card_model->coachList($this->store_id,$dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-04 16:41
     * @功能说明:课程排课列表
     */
    public function appClassDateList(){

        $input = $this->_param;

        $dis[] = ['store_id','=',$this->store_id];

        if(!empty($input['cate_id'])){

            $dis[] = ['cate_id','=',$input['cate_id']];
        }

        $dis[] = ['status','>',-1];

        if(!empty($input['day'])){

            $start_time = strtotime($input['day']);

            $end_time   = $start_time+86400-1;

            $dis[] = ['start_time','between',"$start_time,$end_time"];
        }

        $data = $this->date_model->where($dis)->order('start_time,id')->paginate(10)->toArray();

        if(!empty($data['data'])){

            $cate_model = new ClassCate();

            foreach ($data['data'] as &$v){

                $v['cate_name'] = $cate_model->where(['id'=>$v['cate_id']])->value('title');

                $v['time_text'] = date('H:i',$v['start_time']).'-'.date('H:i',$v['end_time']);

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-15 14:40
     * @功能说明:删除排课
     */
    public function delClassDate(){

        $input = $this->_input;

        $res = $this->date_model->delClassDate($input['id']);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-10 17:27
     * @功能说明:修改店主的权限（店主店员）
     */
    public function updateCoachAuth(){

        $input = $this->_input;

        $user_model = new IndexUserInfo();

        $dis = [

            'fans_id' => $input['fans_id']
        ];

        $res = $user_model->where($dis)->update(['is_class'=>$input['is_class']]);

        return $this->success($res);
    }

    /**
     * @author chenniang
     * @DataTime: 2021-06-10 17:27
     * @功能说明:修改店主的权限（店主店员）
     */
    public function updateCoachStatus(){

        $input = $this->_input;

        $user_model = new IndexUserInfo();

        $dis = [

            'fans_id' => $input['fans_id']
        ];

        $res = $user_model->where($dis)->update(['class_status'=>$input['class_status']]);

        return $this->success($res);
    }




}
